/**
 * 
 */
package cc.dogtailflower.framework.resultsetmapper;

import java.lang.reflect.Field;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

import cc.dogtailflower.framework.annotation.Column;
import cc.dogtailflower.framework.annotation.Entity;
import cc.dogtailflower.framework.annotation.Id;
import cc.dogtailflower.framework.handler.ClassHandler;

/**
 * 数据库 表和对象 的映射表
 * 所有加 Entity 注解的类
 * @author cc
 *
 */
public final class ResultSetObjectMappingTable {

	private static final Map<Class<?>,Map<String,String>> RESULT_MAP = new HashMap<Class<?>,Map<String,String>>();
	private static final Map<String,Map<String,String>> RE_RESULT_MAP = new HashMap<String,Map<String,String>>();
	private static final Map<Class<?>,String> CLASS_TABLE_MAP = new HashMap<Class<?>,String>();
	private static final Map<Class<?>,String> KEY_PROPERTY = new HashMap<Class<?>,String>();
	
	static{	
		Set<Class<?>> entityClassSet = ClassHandler.getEntityClassSet();
		for(Class<?> cls : entityClassSet){
			Map<String,String> columnMap = new HashMap<String,String>();
			Map<String,String> fieldMap = new HashMap<String,String>();
			Entity entity = cls.getAnnotation(Entity.class);
			String TABLE_NAME = entity.table();
			CLASS_TABLE_MAP.put(cls,TABLE_NAME);
			Field[] fields = cls.getDeclaredFields();
			for(Field field : fields){
				if(field.isAnnotationPresent(Id.class)){
					KEY_PROPERTY.put(cls, field.getName());
				}
				Column column = field.getAnnotation(Column.class);
				String columnName = column.name();
				String fieldName = field.getName();
				columnMap.put(columnName, fieldName);
				fieldMap.put(fieldName, columnName);
			}
			RESULT_MAP.put(cls, columnMap);
			RE_RESULT_MAP.put(TABLE_NAME, fieldMap);
		}
	}
	
	public static Map<Class<?>,Map<String,String>> getMap(){
		return RESULT_MAP;
	}
	
	public static Map<String,Map<String,String>> getReMap(){
		return RE_RESULT_MAP;
	}
	
	public static Map<Class<?>,String>getTableMap(){
		return CLASS_TABLE_MAP;
	}
	
	public static Map<Class<?>,String>getKeyMap(){
		return KEY_PROPERTY;
	}
}
